<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\IndexController;
use app\controller\wmapi\order\WaPurchasedVehiclesController;
use app\controller\wmapi\order\OrderPurchaseController;

use app\controller\wmapi\PublicCommentController;
use support\Db;
use Illuminate\Database\Eloquent\Model;
use function MongoDB\BSON\toJSON;

class WaPurchasedVehicles
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_purchased_vehicles';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;


    /**
     * @param $page
     * @param $pageNum
     * @param $purchasecontractsn
     * @param string[] $fields
     * @param array $condition
     * @param array $between
     * @return false|string
     */
    public static function list($page, $pageNum, $fields, $condition = [], $between = [])
    {
        $offset = ($page - 1) * $pageNum;
        if ($between) {
            $model = Db::table('wa_purchased_vehicles_relevance as rel')
                ->leftJoin('wa_purchased_vehicles as ve', 've.vin', '=', 'rel.vin')
                ->select($fields)
                ->where($condition)
                ->whereBetween('rel.confirm_time', $between);
        } else {
            $model = Db::table('wa_purchased_vehicles_relevance as rel')
                ->leftJoin('wa_purchased_vehicles as ve', 've.vin', '=', 'rel.vin')
                ->select($fields)
                ->where($condition);
        }

        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
        if ($list) {

            foreach ($list as $k) {
                // 车辆状况(是否是新车)  1 新车 2 二手车
                if ($k->vehicle_status == 1) {
                    $k->vehicle_status_name = '新车';
                } elseif ($k->vehicle_status == 2) {
                    $k->vehicle_status_name = '二手车';
                } else {
                    $k->vehicle_status_name = '';
                }

                if ($k->collection_shipment_wait_voucher) {
                    $k->collection_shipment_wait_voucher = json_decode($k->collection_shipment_wait_voucher);
                    $k->vehicle_supplier_id = $k->collection_shipment_wait_voucher->supplier_id;
                    $k->supplier_1 = IndexController::supplierDetailCommon(1,$k->vehicle_supplier_id);
                    $supplier_1 = json_decode($k->supplier_1);
                    if ($supplier_1){
                        $k->vehicle_supplier_name = $supplier_1->name;
                    }else{
                        $k->vehicle_supplier_name = '';
                    }

                }

                if ($k->collection_shipment_voucher) {
                    $k->collection_shipment_voucher = json_decode($k->collection_shipment_voucher);
                }
                if ($k->warehousing_voucher) {
                    $k->warehousing_voucher = json_decode($k->warehousing_voucher);
                    if ($k->warehousing_voucher->vehicle_check_result == 1){
                        $k->warehousing_voucher->vehicle_check_result_name = '完好无损';// 1 完好 2  存在破损
                    }elseif ($k->warehousing_voucher->vehicle_check_result == 2){
                        $k->warehousing_voucher->vehicle_check_result_name = '存在破损';// 1 完好 2  存在破损
                    }
                }
                if ($k->registration_voucher) {
                    $k->registration_voucher = json_decode($k->registration_voucher);
                }
                if ($k->lnvoicing_voucher) {
                    $k->lnvoicing_voucher = json_decode($k->lnvoicing_voucher);
                }
                if ($k->off_voucher) {
                    $k->off_voucher = json_decode($k->off_voucher);
                }
                if ($k->transfer_export_voucher) {
                    $k->transfer_export_voucher = json_decode($k->transfer_export_voucher);
                }
                if ($k->surrender_voucher) {
                    $k->surrender_voucher = json_decode($k->surrender_voucher);
                }
                if ($k->port_shipment_voucher) {
                    $k->port_shipment_voucher = json_decode($k->port_shipment_voucher);
                }


                PublicCommentController::车辆状态;// 车辆状态
                
//                if ($k->status == 1) {
//                    $k->status_name = '集库待发运';
//                } elseif ($k->status == 2) {
//                    $k->status_name = '待验车入库';
//                } elseif ($k->status == 3) {
//                    $k->status_name = '已入库待上牌';
//                } elseif ($k->status == 4) {
//                    $k->status_name = '已上牌待开二手';
//                } elseif ($k->status == 5) {
//                    $k->status_name = '已开票待转移出';
//                } elseif ($k->status == 6) {
//                    $k->status_name = '已转移待注销';
//                } elseif ($k->status == 7) {
//                    $k->status_name = '已注销待退保';
//                } elseif ($k->status == 8) {
//                    $k->status_name = '已退保待集港发';
//                } elseif ($k->status == 9) {
//                    $k->status_name = '已订舱待报关';
//                } elseif ($k->status == 10) {
//                    $k->status_name = '已报关(待装运)';
//                } elseif ($k->status == 11) {
//                    $k->status_name = '已装运(待提单释放)';
//                } elseif ($k->status == 12) {
//                    $k->status_name = '已提单释放(待结关)';
//                } elseif ($k->status == 13) {
//                    $k->status_name = '已结关(待退税)';
//                } elseif ($k->status == 14) {
//                    $k->status_name = '已退税(完成车务)';
//                } elseif ($k->status == 20) {
//                    $k->status_name = '已完成车务';
//                } else {
//                    $k->status_name = '';
//                }

                $k->status_name = IndexController::vehicleStatusName($k->status);
                if ($k->warehousing_status == 1) {
                    $k->warehousing_status_name = '待入库';
                } elseif ($k->warehousing_status == 2) {
                    $k->warehousing_status_name = '已入库';
                } else {
                    $k->warehousing_status_name = '';
                }


//                $vehicles_relevance_log = WaPurchasedVehiclesRelevanceLog::all(['vin' => $k->vin]);
//                $vehicles_relevance_log = json_decode($vehicles_relevance_log);
//                if ($vehicles_relevance_log){
//                    $k->vehicle_progress = $vehicles_relevance_log;
//                }else{
//                    $k->vehicle_progress = '';
//                }
            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }


    /**
     * 采购请款审批-状态修改
     * @param $data
     * @return int
     */
    public static function updated($condition, $data)
    {
        return Db::table('wa_purchased_vehicles')->where($condition)->update($data);
    }


    public static function domesticFind($condition)
    {
        $res = Db::table('wa_purchased_vehicles')
            ->where($condition)
            ->first();
        if ($res) {
            $detail = WaPurchasedVehiclesRelevanceLog::all(['vin' => $res->vin]);
            $detail = json_decode($detail);
            if ($detail) {
                $res->vehicle_progress = $detail;
            } else {
                $res->vehicle_progress = '';
            }

            // 集库待发运
            if ($res->collection_shipment_wait_voucher) {
                $res->collection_shipment_wait_voucher = json_decode($res->collection_shipment_wait_voucher);
                if (!$res->collection_shipment_wait_voucher->supplier_type || $res->collection_shipment_wait_voucher->supplier_id){
                }
                $supplier = IndexController::supplierDetailCommon($res->collection_shipment_wait_voucher->supplier_type, $res->collection_shipment_wait_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->collection_shipment_wait_voucher->supplier_name = $supplier->name;
                }else{
                    $res->collection_shipment_wait_voucher->supplier_name = '';
                }

            }

            // 集库发运
            if ($res->collection_shipment_voucher) {
                $res->collection_shipment_voucher = json_decode($res->collection_shipment_voucher);
                if (!$res->collection_shipment_voucher->supplier_type || $res->collection_shipment_voucher->supplier_id){
                    var_dump('collection_shipment_voucher');
                    var_dump($res->collection_shipment_voucher->supplier_type);
                    var_dump($res->collection_shipment_voucher->supplier_id);
                }
                $res->collection_shipment_voucher->currency = 1; // 币种 1 人民币 2 美元 3 欧元
                $res->collection_shipment_voucher->currency_name = '人民币';
                $supplier = IndexController::supplierDetailCommon($res->collection_shipment_voucher->supplier_type, $res->collection_shipment_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->collection_shipment_voucher->supplier_name = $supplier->name;
                }else{
                    $res->collection_shipment_voucher->supplier_name = '';
                }

            }

            // 确认入库 不需要供应商名称
            if ($res->warehousing_voucher) {
                $res->warehousing_voucher = json_decode($res->warehousing_voucher);
                    if ($res->warehousing_voucher->vehicle_check_result == 1){
                        $res->warehousing_voucher->vehicle_check_result_name = '完好无损';// 1 完好 2  存在破损
                    }elseif ($res->warehousing_voucher->vehicle_check_result == 2){
                        $res->warehousing_voucher->vehicle_check_result_name = '存在破损';// 1 完好 2  存在破损
                    }
            }

            //  确认上牌
            if ($res->registration_voucher) {
                $res->registration_voucher = json_decode($res->registration_voucher);
                if (!$res->registration_voucher->supplier_type || $res->registration_voucher->supplier_id){
                    var_dump('registration_voucher');
                    var_dump($res->registration_voucher->supplier_type);
                    var_dump($res->registration_voucher->supplier_id);
                }
                $res->registration_voucher->currency = 1; // 币种 1 人民币 2 美元 3 欧元
                $res->registration_voucher->currency_name = '人民币';
                $supplier = IndexController::supplierDetailCommon($res->registration_voucher->supplier_type, $res->registration_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if (!$supplier) {
                    return IndexController::errored($supplier, '供应商详情获取失败');
                }
                $res->registration_voucher->supplier_name = $supplier->name;
            }

            // 确认开票
            if ($res->lnvoicing_voucher) {
                $res->lnvoicing_voucher = json_decode($res->lnvoicing_voucher);
                if (!$res->lnvoicing_voucher->supplier_type || $res->lnvoicing_voucher->supplier_id){
                    var_dump('lnvoicing_voucher');
                    var_dump($res->lnvoicing_voucher->supplier_type);
                    var_dump($res->lnvoicing_voucher->supplier_id);
                }
                $res->lnvoicing_voucher->currency = 1; // 币种 1 人民币 2 美元 3 欧元
                $res->lnvoicing_voucher->currency_name = '人民币';
                PublicCommentController::供应商类型;
//                if ($res->lnvoicing_voucher->supplier_type == 1) {
//                    $res->lnvoicing_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->lnvoicing_voucher->supplier_type == 2) {
//                    $res->lnvoicing_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->lnvoicing_voucher->supplier_type == 3) {
//                    $res->lnvoicing_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->lnvoicing_voucher->supplier_type == 4) {
//                    $res->lnvoicing_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->lnvoicing_voucher->supplier_type == 5) {
//                    $res->lnvoicing_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->lnvoicing_voucher->supplier_type_name = '';
//                }
                $res->lnvoicing_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->lnvoicing_voucher->supplier_type);

                $supplier = IndexController::supplierDetailCommon($res->lnvoicing_voucher->supplier_type, $res->lnvoicing_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if (!$supplier) {
                    return IndexController::errored($supplier, '供应商详情获取失败');
                }
                $res->lnvoicing_voucher->supplier_name = $supplier->name;
            }
            // 确认注销  不需要供应商名称
            if ($res->off_voucher) {
                $res->off_voucher = json_decode($res->off_voucher);
            }
            // 确认转移出口凭证
            if ($res->transfer_export_voucher) {
                $res->transfer_export_voucher = json_decode($res->transfer_export_voucher);
                $res->transfer_export_voucher->currency = 1; // 币种 1 人民币 2 美元 3 欧元
                $res->transfer_export_voucher->currency_name = '人民币';
                PublicCommentController::供应商类型;
//                if ($res->transfer_export_voucher->supplier_type == 1) {
//                    $res->transfer_export_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->transfer_export_voucher->supplier_type == 2) {
//                    $res->transfer_export_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->transfer_export_voucher->supplier_type == 3) {
//                    $res->transfer_export_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->transfer_export_voucher->supplier_type == 4) {
//                    $res->transfer_export_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->transfer_export_voucher->supplier_type == 5) {
//                    $res->transfer_export_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->transfer_export_voucher->supplier_type_name = '';
//                }
                $res->transfer_export_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->transfer_export_voucher->supplier_type);

                $supplier = IndexController::supplierDetailCommon($res->transfer_export_voucher->supplier_type, $res->transfer_export_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if (!$supplier) {
                    return IndexController::errored($supplier, '供应商详情获取失败');
                }
                $res->transfer_export_voucher->supplier_name = $supplier->name;
            }

            // 确认退保凭证  不需要供应商名称
            if ($res->surrender_voucher) {
                $res->surrender_voucher = json_decode($res->surrender_voucher);

            }

            // 确认集港发运
            if ($res->port_shipment_voucher) {
                $res->port_shipment_voucher = json_decode($res->port_shipment_voucher);
                if (!$res->port_shipment_voucher->supplier_type || $res->port_shipment_voucher->supplier_id){
                    var_dump('port_shipment_voucher');
                    var_dump($res->port_shipment_voucher->supplier_type);
                    var_dump($res->port_shipment_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->port_shipment_voucher->supplier_type, $res->port_shipment_voucher->supplier_id);
                $supplier = json_decode($supplier);
                if (!$supplier) {
                    return IndexController::errored($supplier, '供应商详情获取失败');
                }
                $res->port_shipment_voucher->supplier_name = $supplier->name;
            }


            PublicCommentController::车辆状态;// 车辆状态
//            if ($res->status == 1) {
//                $res->status_name = '集库待发运';
//            } elseif ($res->status == 2) {
//                $res->status_name = '待验车入库';
//            } elseif ($res->status == 3) {
//                $res->status_name = '已入库待上牌';
//            } elseif ($res->status == 4) {
//                $res->status_name = '已上牌待开二手';
//            } elseif ($res->status == 5) {
//                $res->status_name = '已开票待转移出';
//            } elseif ($res->status == 6) {
//                $res->status_name = '已转移待注销';
//            } elseif ($res->status == 7) {
//                $res->status_name = '已注销待退保';
//            } elseif ($res->status == 8) {
//                $res->status_name = '已退保待集港发';
//            } else {
//                $res->status_name = '';
//            }
            $res->status_name = IndexController::vehicleStatusName($res->status);

            if ($res->warehousing_status == 1) {
                $res->warehousing_status_name = '待入库';
            } elseif ($res->warehousing_status == 2) {
                $res->warehousing_status_name = '已入库';
            } else {
                $res->warehousing_status_name = '';
            }

            // 车辆状况(是否是新车)  1 新车 2 二手车
            if ($res->vehicle_status == 1) {
                $res->vehicle_status_name = '新车';
            } elseif ($res->vehicle_status == 2) {
                $res->vehicle_status_name = '二手车';
            } else {
                $res->vehicle_status_name = '';
            }
        }
        return json_encode($res);
    }

    public static function interFind($condition)
    {
        $res = Db::table('wa_purchased_vehicles')
            ->where($condition)
            ->first();
        if ($res) {

            $detail = WaPurchasedVehiclesRelevanceLog::all(['vin' => $res->vin]);
            $detail = json_decode($detail);
            if ($detail) {
                $res->vehicle_progress = $detail;
            } else {
                $res->vehicle_progress = '';
            }

            //9 确认订舱
            if ($res->booking_space_voucher) {
                $res->booking_space_voucher = json_decode($res->booking_space_voucher);
                if (!$res->booking_space_voucher->supplier_type || $res->booking_space_voucher->supplier_id){
                    var_dump('booking_space_voucher');
                    var_dump($res->booking_space_voucher->supplier_type);
                    var_dump($res->booking_space_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->booking_space_voucher->supplier_type, $res->booking_space_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->booking_space_voucher->supplier_name = $supplier->name;
                }else{
                    $res->booking_space_voucher->supplier_name = '';
                }
                
                //币种 1 人民币 2 美元 3 欧元
//                if ($res->booking_space_voucher->inter_currency == 1) {
//                    $res->booking_space_voucher->inter_currency_name = '人民币';
//                }elseif($res->booking_space_voucher->inter_currency == 2){
//                    $res->booking_space_voucher->inter_currency_name = '美元';
//                }elseif($res->booking_space_voucher->inter_currency == 3){
//                    $res->booking_space_voucher->inter_currency_name = '欧元';
//                }else{
//                    $res->booking_space_voucher->inter_currency_name = '其它';
//                }

                $res->booking_space_voucher->inter_currency_name = IndexController::currencyNameCommon($res->booking_space_voucher->inter_currency);


                //币种 1 人民币 2 美元 3 欧元
//                if ($res->booking_space_voucher->ground_currency == 1) {
//                    $res->booking_space_voucher->ground_currency_name = '人民币';
//                }elseif($res->booking_space_voucher->ground_currency == 2){
//                    $res->booking_space_voucher->ground_currency_name = '美元';
//                }elseif($res->booking_space_voucher->ground_currency == 3){
//                    $res->booking_space_voucher->ground_currency_name = '欧元';
//                }else{
//                    $res->booking_space_voucher->ground_currency_name = '其它';
//                }
                $res->booking_space_voucher->ground_currency_name = IndexController::currencyNameCommon($res->booking_space_voucher->ground_currency);

//                if ($res->booking_space_voucher->supplier_type == 1) {
//                    $res->booking_space_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->booking_space_voucher->supplier_type == 2) {
//                    $res->booking_space_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->booking_space_voucher->supplier_type == 3) {
//                    $res->booking_space_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->booking_space_voucher->supplier_type == 4) {
//                    $res->booking_space_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->booking_space_voucher->supplier_type == 5) {
//                    $res->booking_space_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->booking_space_voucher->supplier_type_name = '';
//                }
                $res->booking_space_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->booking_space_voucher->supplier_type);


            }

            // 10 确认报关(待装运)
            if ($res->declare_customs_voucher) {
                $res->declare_customs_voucher = json_decode($res->declare_customs_voucher);

                if (!$res->declare_customs_voucher->supplier_type || $res->declare_customs_voucher->supplier_id){
                    var_dump('declare_customs_voucher');
                    var_dump($res->declare_customs_voucher->supplier_type);
                    var_dump($res->declare_customs_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->declare_customs_voucher->supplier_type, $res->declare_customs_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->declare_customs_voucher->supplier_name = $supplier->name;
                }else{
                    $res->declare_customs_voucher->supplier_name = '';
                }
                
                //币种 1 人民币 2 美元 3 欧元
//                if ($res->declare_customs_voucher->currency == 1) {
//                    $res->declare_customs_voucher->currency_name = '人民币';
//                }elseif($res->declare_customs_voucher->currency == 2){
//                    $res->declare_customs_voucher->currency_name = '美元';
//                }elseif($res->declare_customs_voucher->currency == 3){
//                    $res->declare_customs_voucher->currency_name = '欧元';
//                }else{
//                    $res->declare_customs_voucher->currency_name = '其它';
//                }
                $res->declare_customs_voucher->currency_name = IndexController::currencyNameCommon($res->declare_customs_voucher->currency);

                PublicCommentController::供应商类型;
//                if ($res->declare_customs_voucher->supplier_type == 1) {
//                    $res->declare_customs_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->declare_customs_voucher->supplier_type == 2) {
//                    $res->declare_customs_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->declare_customs_voucher->supplier_type == 3) {
//                    $res->declare_customs_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->declare_customs_voucher->supplier_type == 4) {
//                    $res->declare_customs_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->declare_customs_voucher->supplier_type == 5) {
//                    $res->declare_customs_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->declare_customs_voucher->supplier_type_name = '';
//                }
                $res->declare_customs_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->declare_customs_voucher->supplier_type);


                $supplier = IndexController::supplierDetailCommon($res->declare_customs_voucher->supplier_type, $res->declare_customs_voucher->supplier_id);
                $supplier = json_decode($supplier);
                if ($supplier) {
                    $res->declare_customs_voucher->supplier_name = $supplier->name;
                }else{
                    $res->declare_customs_voucher->supplier_name = '';
                }

            }

            // 11 确认装运(待提单释放)  shipment_voucher
            if ($res->shipment_voucher) {
                $res->shipment_voucher = json_decode($res->shipment_voucher);
                if (!$res->shipment_voucher->supplier_type || $res->shipment_voucher->supplier_id){
                    var_dump('shipment_voucher');
                    var_dump($res->shipment_voucher->supplier_type);
                    var_dump($res->shipment_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->shipment_voucher->supplier_type, $res->shipment_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->shipment_voucher->supplier_name = $supplier->name;
                }else{
                    $res->shipment_voucher->supplier_name = '';
                }
                //币种 1 人民币 2 美元 3 欧元
//                if ($res->shipment_voucher->currency == 1) {
//                    $res->shipment_voucher->currency_name = '人民币';
//                }elseif($res->shipment_voucher->currency == 2){
//                    $res->shipment_voucher->currency_name = '美元';
//                }elseif($res->shipment_voucher->currency == 3){
//                    $res->shipment_voucher->currency_name = '欧元';
//                }else{
//                    $res->shipment_voucher->currency_name = '其它';
//                }
                $res->shipment_voucher->currency_name = IndexController::currencyNameCommon($res->shipment_voucher->currency);

                PublicCommentController::供应商类型;
//                if ($res->shipment_voucher->supplier_type == 1) {
//                    $res->shipment_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->shipment_voucher->supplier_type == 2) {
//                    $res->shipment_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->shipment_voucher->supplier_type == 3) {
//                    $res->shipment_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->shipment_voucher->supplier_type == 4) {
//                    $res->shipment_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->shipment_voucher->supplier_type == 5) {
//                    $res->shipment_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->shipment_voucher->supplier_type_name = '';
//                }
                $res->shipment_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->shipment_voucher->supplier_type);


//                if ($res->shipment_voucher->vehicle_check_result == 1){
//                    $res->shipment_voucher->vehicle_check_result_name = '完好无损';// 1 完好 2  存在破损
//                }elseif ($res->shipment_voucher->vehicle_check_result == 2){
//                    $res->shipment_voucher->vehicle_check_result_name = '存在破损';// 1 完好 2  存在破损
//                }
            }

            //  12 确认提单释放(待结关)  release_bill_lading_voucher
            if ($res->release_bill_lading_voucher) {
                $res->release_bill_lading_voucher = json_decode($res->release_bill_lading_voucher);

                $res->release_bill_lading_voucher->supplier_name = $supplier->name;
            }

            // 确认开票  13 确认结关(待退税) customs_clearance_voucher
            if ($res->customs_clearance_voucher) {
                $res->customs_clearance_voucher = json_decode($res->customs_clearance_voucher);
                $res->customs_clearance_voucher->supplier_name = $supplier->name;
            }
            // 确认注销  不需要供应商名称   14 确认退税(完成) drawback_voucher
            if ($res->drawback_voucher) {
                $res->drawback_voucher = json_decode($res->drawback_voucher);
            }



            PublicCommentController::车辆状态;// 车辆状态
            if ($res->status == 9) {
                $res->status_name = '已订舱待报关';
            } elseif ($res->status == 10) {
                $res->status_name = '已报关(待装运)';
            } elseif ($res->status == 11) {
                $res->status_name = '已装运(待提单释放)';
            } elseif ($res->status == 12) {
                $res->status_name = '已提单释放(待结关)';
            } elseif ($res->status == 13) {
                $res->status_name = '已结关(待退税)';
            } elseif ($res->status == 14) {
                $res->status_name = '已退税(完成车务)';
            } elseif ($res->status == 20) {
                $res->status_name = '已完成车务';
            } else {
                $res->status_name = '';
            }
            // 车辆状况(是否是新车)  1 新车 2 二手车
            if ($res->vehicle_status == 1) {
                $res->vehicle_status_name = '新车';
            } elseif ($res->vehicle_status == 2) {
                $res->vehicle_status_name = '二手车';
            } else {
                $res->vehicle_status_name = '';
            }

        }
        return json_encode($res);
    }

    public static function find($condition)
    {
        $res = Db::table('wa_purchased_vehicles')
            ->where($condition)
            ->first();
        if ($res) {
            $detail = WaPurchasedVehiclesRelevanceLog::all(['vin' => $res->vin]);
            $detail = json_decode($detail);
            if ($detail) {
                $res->vehicle_progress = $detail;
            } else {
                $res->vehicle_progress = '';
            }

            // 集库待发运
            if ($res->collection_shipment_wait_voucher) {
                $res->collection_shipment_wait_voucher = json_decode($res->collection_shipment_wait_voucher);
                if (!$res->collection_shipment_wait_voucher->supplier_type || !$res->collection_shipment_wait_voucher->supplier_id){
                    var_dump('collection_shipment_wait_voucher');
                    var_dump($res->collection_shipment_wait_voucher->supplier_type);
                    var_dump($res->collection_shipment_wait_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->collection_shipment_wait_voucher->supplier_type, $res->collection_shipment_wait_voucher->supplier_id);
                $supplier = json_decode($supplier);
                if ($supplier) {
                    $res->collection_shipment_wait_voucher->supplier_name = $supplier->name;
                }else{
                    $res->collection_shipment_wait_voucher->supplier_name = '';
                }

            }

            // 集库发运
            if ($res->collection_shipment_voucher) {
                $res->collection_shipment_voucher = json_decode($res->collection_shipment_voucher);
                if (!$res->collection_shipment_voucher->supplier_type || !$res->collection_shipment_voucher->supplier_id){
                    var_dump('collection_shipment_voucher');
                    var_dump($res->collection_shipment_voucher->supplier_type);
                    var_dump($res->collection_shipment_voucher->supplier_id);
                }
                $res->collection_shipment_voucher->currency = 1; // 币种 1 人民币 2 美元 3 欧元
                $res->collection_shipment_voucher->currency_name = '人民币';
                $supplier = IndexController::supplierDetailCommon($res->collection_shipment_voucher->supplier_type, $res->collection_shipment_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->collection_shipment_voucher->supplier_name = $supplier->name;
                }else{
                    $res->collection_shipment_voucher->supplier_name = '';
                }

            }

            // 确认入库 不需要供应商名称
            if ($res->warehousing_voucher) {
                $res->warehousing_voucher = json_decode($res->warehousing_voucher);

                if ($res->warehousing_voucher->vehicle_check_result == 1){
                    $res->warehousing_voucher->vehicle_check_result_name = '完好无损';// 1 完好 2  存在破损
                }elseif ($res->warehousing_voucher->vehicle_check_result == 2){
                    $res->warehousing_voucher->vehicle_check_result_name = '存在破损';// 1 完好 2  存在破损
                }
            }


            //  确认上牌
            if ($res->registration_voucher) {
                $res->registration_voucher = json_decode($res->registration_voucher);
                if (!$res->registration_voucher->supplier_type || !$res->registration_voucher->supplier_id){
                    var_dump('registration_voucher');
                    var_dump($res->registration_voucher->supplier_type);
                    var_dump($res->registration_voucher->supplier_id);
                }
                $res->registration_voucher->currency = 1; // 币种 1 人民币 2 美元 3 欧元
                $res->registration_voucher->currency_name = '人民币';
                $supplier = IndexController::supplierDetailCommon($res->registration_voucher->supplier_type, $res->registration_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if (!$supplier) {
                    return IndexController::errored($supplier, '供应商详情获取失败');
                }
                $res->registration_voucher->supplier_name = $supplier->name;
            }


            // 确认开票
            if ($res->lnvoicing_voucher) {
                $res->lnvoicing_voucher = json_decode($res->lnvoicing_voucher);
                if (!$res->lnvoicing_voucher->supplier_type || !$res->lnvoicing_voucher->supplier_id){
                    var_dump('lnvoicing_voucher');
                    var_dump($res->lnvoicing_voucher->supplier_type);
                    var_dump($res->lnvoicing_voucher->supplier_id);
                }
                $res->lnvoicing_voucher->currency = 1; // 币种 1 人民币 2 美元 3 欧元
                $res->lnvoicing_voucher->currency_name = '人民币';
                PublicCommentController::供应商类型;
//                if ($res->lnvoicing_voucher->supplier_type == 1) {
//                    $res->lnvoicing_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->lnvoicing_voucher->supplier_type == 2) {
//                    $res->lnvoicing_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->lnvoicing_voucher->supplier_type == 3) {
//                    $res->lnvoicing_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->lnvoicing_voucher->supplier_type == 4) {
//                    $res->lnvoicing_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->lnvoicing_voucher->supplier_type == 5) {
//                    $res->lnvoicing_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->lnvoicing_voucher->supplier_type_name = '';
//                }
                $res->lnvoicing_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->lnvoicing_voucher->supplier_type);



                $supplier = IndexController::supplierDetailCommon($res->lnvoicing_voucher->supplier_type, $res->lnvoicing_voucher->supplier_id);
                $supplier = json_decode($supplier);
                if (!$supplier) {
                    return IndexController::errored($supplier, '供应商详情获取失败');
                }

                $res->lnvoicing_voucher->supplier_name = $supplier->name;
            }



            // 确认注销  不需要供应商名称
            if ($res->off_voucher) {
                $res->off_voucher = json_decode($res->off_voucher);
            }
            // 确认转移出口凭证
            if ($res->transfer_export_voucher) {
                $res->transfer_export_voucher = json_decode($res->transfer_export_voucher);
                if (!$res->transfer_export_voucher->supplier_type || !$res->transfer_export_voucher->supplier_id){
                    var_dump('transfer_export_voucher');
                    var_dump($res->transfer_export_voucher->supplier_type);
                    var_dump($res->transfer_export_voucher->supplier_id);
                }
                $res->transfer_export_voucher->currency = 1; // 币种 1 人民币 2 美元 3 欧元
                $res->transfer_export_voucher->currency_name = '人民币';
                PublicCommentController::供应商类型;
//                if ($res->transfer_export_voucher->supplier_type == 1) {
//                    $res->transfer_export_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->transfer_export_voucher->supplier_type == 2) {
//                    $res->transfer_export_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->transfer_export_voucher->supplier_type == 3) {
//                    $res->transfer_export_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->transfer_export_voucher->supplier_type == 4) {
//                    $res->transfer_export_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->transfer_export_voucher->supplier_type == 5) {
//                    $res->transfer_export_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->transfer_export_voucher->supplier_type_name = '';
//                }
                $res->transfer_export_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->transfer_export_voucher->supplier_type);



                $supplier = IndexController::supplierDetailCommon($res->transfer_export_voucher->supplier_type, $res->transfer_export_voucher->supplier_id);
                $supplier = json_decode($supplier);
                if (!$supplier) {
                    return IndexController::errored($supplier, '供应商详情获取失败');
                }

                $res->transfer_export_voucher->supplier_name = $supplier->name;
            }



            // 确认退保凭证  不需要供应商名称
            if ($res->surrender_voucher) {
                $res->surrender_voucher = json_decode($res->surrender_voucher);

            }



            // 确认集港发运
            if ($res->port_shipment_voucher) {
                $res->port_shipment_voucher = json_decode($res->port_shipment_voucher);
                if (!$res->port_shipment_voucher->supplier_type || !$res->port_shipment_voucher->supplier_id){
                    var_dump('port_shipment_voucher');
                    var_dump($res->port_shipment_voucher->supplier_type);
                    var_dump($res->port_shipment_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->port_shipment_voucher->supplier_type, $res->port_shipment_voucher->supplier_id);
                $supplier = json_decode($supplier);
                if (!$supplier) {
                    return IndexController::errored($supplier, '供应商详情获取失败');
                }

                $res->port_shipment_voucher->supplier_name = $supplier->name;
            }



            //9 确认订舱
            if ($res->booking_space_voucher) {
                $res->booking_space_voucher = json_decode($res->booking_space_voucher);
                if (!$res->booking_space_voucher->supplier_type || !$res->booking_space_voucher->supplier_id){
                    var_dump('booking_space_voucher');
                    var_dump($res->booking_space_voucher->supplier_type);
                    var_dump($res->booking_space_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->booking_space_voucher->supplier_type, $res->booking_space_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->booking_space_voucher->supplier_name = $supplier->name;
                }else{
                    $res->booking_space_voucher->supplier_name = '';
                }

                //币种 1 人民币 2 美元 3 欧元
//                if ($res->booking_space_voucher->inter_currency == 1) {
//                    $res->booking_space_voucher->inter_currency_name = '人民币';
//                }elseif($res->booking_space_voucher->inter_currency == 2){
//                    $res->booking_space_voucher->inter_currency_name = '美元';
//                }elseif($res->booking_space_voucher->inter_currency == 3){
//                    $res->booking_space_voucher->inter_currency_name = '欧元';
//                }else{
//                    $res->booking_space_voucher->inter_currency_name = '其它';
//                }
                $res->booking_space_voucher->inter_currency_name = IndexController::currencyNameCommon($res->booking_space_voucher->inter_currency);

                //币种 1 人民币 2 美元 3 欧元
//                if ($res->booking_space_voucher->ground_currency == 1) {
//                    $res->booking_space_voucher->ground_currency_name = '人民币';
//                }elseif($res->booking_space_voucher->ground_currency == 2){
//                    $res->booking_space_voucher->ground_currency_name = '美元';
//                }elseif($res->booking_space_voucher->ground_currency == 3){
//                    $res->booking_space_voucher->ground_currency_name = '欧元';
//                }else{
//                    $res->booking_space_voucher->ground_currency_name = '其它';
//                }
                $res->booking_space_voucher->ground_currency_name = IndexController::currencyNameCommon($res->booking_space_voucher->ground_currency);

//                if ($res->booking_space_voucher->supplier_type == 1) {
//                    $res->booking_space_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->booking_space_voucher->supplier_type == 2) {
//                    $res->booking_space_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->booking_space_voucher->supplier_type == 3) {
//                    $res->booking_space_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->booking_space_voucher->supplier_type == 4) {
//                    $res->booking_space_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->booking_space_voucher->supplier_type == 5) {
//                    $res->booking_space_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->booking_space_voucher->supplier_type_name = '';
//                }

                $res->booking_space_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->booking_space_voucher->supplier_type);


                $supplier = IndexController::supplierDetailCommon($res->booking_space_voucher->supplier_type, $res->booking_space_voucher->supplier_id);
                $supplier = json_decode($supplier);
                if ($supplier) {

                    $res->booking_space_voucher->supplier_name = $supplier->name;
                }else{
                    $res->booking_space_voucher->supplier_name = '';
                }

            }



            // 10 确认报关(待装运)
            if ($res->declare_customs_voucher) {
                $res->declare_customs_voucher = json_decode($res->declare_customs_voucher);
                if (!$res->declare_customs_voucher->supplier_type || !$res->declare_customs_voucher->supplier_id){
                    var_dump('declare_customs_voucher');
                    var_dump($res->declare_customs_voucher->supplier_type);
                    var_dump($res->declare_customs_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->declare_customs_voucher->supplier_type, $res->declare_customs_voucher->supplier_id);
                $supplier = json_decode($supplier);
                if ($supplier) {
                    $res->declare_customs_voucher->supplier_name = $supplier->name;
                }else{
                    $res->declare_customs_voucher->supplier_name = '';
                }



                //币种 1 人民币 2 美元 3 欧元
//                if ($res->declare_customs_voucher->currency == 1) {
//                    $res->declare_customs_voucher->currency_name = '人民币';
//                }elseif($res->declare_customs_voucher->currency == 2){
//                    $res->declare_customs_voucher->currency_name = '美元';
//                }elseif($res->declare_customs_voucher->currency == 3){
//                    $res->declare_customs_voucher->currency_name = '欧元';
//                }else{
//                    $res->declare_customs_voucher->currency_name = '其它';
//                }
                $res->declare_customs_voucher->currency_name = IndexController::currencyNameCommon($res->declare_customs_voucher->currency);

                PublicCommentController::供应商类型;
//                if ($res->declare_customs_voucher->supplier_type == 1) {
//                    $res->declare_customs_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->declare_customs_voucher->supplier_type == 2) {
//                    $res->declare_customs_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->declare_customs_voucher->supplier_type == 3) {
//                    $res->declare_customs_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->declare_customs_voucher->supplier_type == 4) {
//                    $res->declare_customs_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->declare_customs_voucher->supplier_type == 5) {
//                    $res->declare_customs_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->declare_customs_voucher->supplier_type_name = '';
//                }

                $res->declare_customs_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->declare_customs_voucher->supplier_type);


                $supplier = IndexController::supplierDetailCommon($res->declare_customs_voucher->supplier_type, $res->declare_customs_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->declare_customs_voucher->supplier_name = $supplier->name;
                }else{
                    $res->declare_customs_voucher->supplier_name = '';
                }

            }



            // 11 确认装运(待提单释放)  shipment_voucher
            if ($res->shipment_voucher) {
                $res->shipment_voucher = json_decode($res->shipment_voucher);
                if (!$res->shipment_voucher->supplier_type || $res->shipment_voucher->supplier_id){
                    var_dump('shipment_voucher');
                    var_dump($res->shipment_voucher->supplier_type);
                    var_dump($res->shipment_voucher->supplier_id);
                }
                $supplier = IndexController::supplierDetailCommon($res->shipment_voucher->supplier_type, $res->shipment_voucher->supplier_id);
                $supplier = json_decode($supplier);

                if ($supplier) {
                    $res->shipment_voucher->supplier_name = $supplier->name;
                }else{
                    $res->shipment_voucher->supplier_name = '';
                }


                //币种 1 人民币 2 美元 3 欧元
//                if ($res->shipment_voucher->currency == 1) {
//                    $res->shipment_voucher->currency_name = '人民币';
//                }elseif($res->shipment_voucher->currency == 2){
//                    $res->shipment_voucher->currency_name = '美元';
//                }elseif($res->shipment_voucher->currency == 3){
//                    $res->shipment_voucher->currency_name = '欧元';
//                }else{
//                    $res->shipment_voucher->currency_name = '其它';
//                }
                $res->shipment_voucher->currency_name = IndexController::currencyNameCommon($res->shipment_voucher->currency);

                PublicCommentController::供应商类型;
//                if ($res->shipment_voucher->supplier_type == 1) {
//                    $res->shipment_voucher->supplier_type_name = '车辆供应商';
//                } elseif ($res->shipment_voucher->supplier_type == 2) {
//                    $res->shipment_voucher->supplier_type_name = '上牌供应商';
//                } elseif ($res->shipment_voucher->supplier_type == 3) {
//                    $res->shipment_voucher->supplier_type_name = '国际货运代理';
//                } elseif ($res->shipment_voucher->supplier_type == 4) {
//                    $res->shipment_voucher->supplier_type_name = '保险供应商';
//                } elseif ($res->shipment_voucher->supplier_type == 5) {
//                    $res->shipment_voucher->supplier_type_name = '国内物流';
//                } else {
//                    $res->shipment_voucher->supplier_type_name = '';
//                }

                $res->shipment_voucher->supplier_type_name = IndexController::supplierTypeNameCommon($res->shipment_voucher->supplier_type);



//                if ($res->shipment_voucher->vehicle_check_result == 1){
//                    $res->shipment_voucher->vehicle_check_result_name = '完好无损';// 1 完好 2  存在破损
//                }elseif ($res->shipment_voucher->vehicle_check_result == 2){
//                    $res->shipment_voucher->vehicle_check_result_name = '存在破损';// 1 完好 2  存在破损
//                }
            }

            //  12 确认提单释放(待结关)  release_bill_lading_voucher
            if ($res->release_bill_lading_voucher) {
                $res->release_bill_lading_voucher = json_decode($res->release_bill_lading_voucher);

//                $res->release_bill_lading_voucher->supplier_name = $supplier->name;
            }

            // 13 确认结关(待退税) customs_clearance_voucher
            if ($res->customs_clearance_voucher) {
                $res->customs_clearance_voucher = json_decode($res->customs_clearance_voucher);
//                $res->customs_clearance_voucher->supplier_name = $supplier->name;
            }
            // 14 确认退税(完成) drawback_voucher
            if ($res->drawback_voucher) {
                $res->drawback_voucher = json_decode($res->drawback_voucher);
            }




            PublicCommentController::车辆状态;// 车辆状态
//            if ($res->status == 1) {
//                $res->status_name = '集库待发运';
//            } elseif ($res->status == 2) {
//                $res->status_name = '待验车入库';
//            } elseif ($res->status == 3) {
//                $res->status_name = '已入库待上牌';
//            } elseif ($res->status == 4) {
//                $res->status_name = '已上牌待开二手';
//            } elseif ($res->status == 5) {
//                $res->status_name = '已开票待转移出';
//            } elseif ($res->status == 6) {
//                $res->status_name = '已转移待注销';
//            } elseif ($res->status == 7) {
//                $res->status_name = '已注销待退保';
//            } elseif ($res->status == 8) {
//                $res->status_name = '已退保待集港发运';
//            } elseif ($res->status == 9) {
//                $res->status_name = '已订舱待报关';
//            } elseif ($res->status == 10) {
//                $res->status_name = '已报关(待装运)';
//            } elseif ($res->status == 11) {
//                $res->status_name = '已装运(待提单释放)';
//            } elseif ($res->status == 12) {
//                $res->status_name = '已提单释放(待结关)';
//            } elseif ($res->status == 13) {
//                $res->status_name = '已结关(待退税)';
//            } elseif ($res->status == 14) {
//                $res->status_name = '已退税(完成车务)';
//            } elseif ($res->status == 20) {
//                $res->status_name = '已完成车务';
//            } else {
//                $res->status_name = '';
//            }
            $res->status_name = IndexController::vehicleStatusName($res->status);

            if ($res->warehousing_status == 1) {
                $res->warehousing_status_name = '待入库';
            } elseif ($res->warehousing_status == 2) {
                $res->warehousing_status_name = '已入库';
            } else {
                $res->warehousing_status_name = '';
            }

            // 车辆状况(是否是新车)  1 新车 2 二手车
            if ($res->vehicle_status == 1) {
                $res->vehicle_status_name = '新车';
            } elseif ($res->vehicle_status == 2) {
                $res->vehicle_status_name = '二手车';
            } else {
                $res->vehicle_status_name = '';
            }
        }
        return json_encode($res);
    }

    /**
     * 采购请款审批 -创建
     * @param $data
     * @return int
     */
    public static function created($data)
    {
        return Db::table('wa_purchased_vehicles')->insertGetId($data);
    }

    public static function findany($condition, $between)
    {
        if ($between) {
            $res = Db::table('wa_purchased_vehicles as vehicles')
//                ->leftJoin('wa_orders_operate_log_vin as vinlog', 'vinlog.vin', '!=', 'vehicles.vin')
                ->select('vehicles.*')
                ->where($condition)
                ->whereBetween('vehicles.status', $between)
                ->get();

        } else {
            $res = Db::table('wa_purchased_vehicles as vehicles')
//                ->leftJoin('wa_orders_operate_log_vin as vinlog', 'vinlog.vin', '!=', 'vehicles.vin')
                ->select('vehicles.*')
                ->where($condition)
                ->get();
        }

        return json_encode($res);
    }

    public static function findByColumnName($field,$condition)
    {

        $res = Db::table('wa_purchased_vehicles')
            ->select($field)
            ->where($condition)
            ->first();
        if ($res){
            // 车辆状况(是否是新车)  1 新车 2 二手车
            if ($res->vehicle_status == 1 ){
                $res->vehicle_status_name = '新车';
            }elseif ($res->vehicle_status == 2){
                $res->vehicle_status_name = '二手车';
            }else{
                $res->vehicle_status_name = '';
            }

        }

        return json_encode($res);
    }


    public static function all($file, $where)
    {
        $list = Db::table('wa_purchased_vehicles')->select($file)->where($where)
            ->get();
        if ($list) {

            foreach ($list as $k) {
//                if ($k->voucher) {
//                    $k->voucher = json_decode($k->voucher);
//                } else {
//                    $k->voucher = [];
//                }
                PublicCommentController::车辆状态;// 车辆状态
//                if ($k->status == 1) {
//                    $k->status_name = '集库待发运';
//                } elseif ($k->status == 2) {
//                    $k->status_name = '待验车入库';
//                } elseif ($k->status == 3) {
//                    $k->status_name = '已入库待上牌';
//                } elseif ($k->status == 4) {
//                    $k->status_name = '已上牌待开二手';
//                } elseif ($k->status == 5) {
//                    $k->status_name = '已开票待转移出';
//                } elseif ($k->status == 6) {
//                    $k->status_name = '已转移待注销';
//                } elseif ($k->status == 7) {
//                    $k->status_name = '已注销待退保';
//                } elseif ($k->status == 8) {
//                    $k->status_name = '已退保待集港发';
//                } else {
//                    $k->status_name = '';
//                }
                $k->status_name = IndexController::vehicleStatusName($k->status);



                if ($k->warehousing_status == 1) {
                    $k->warehousing_status_name = '待入库';
                } elseif ($k->warehousing_status == 2) {
                    $k->warehousing_status_name = '已入库';
                } else {
                    $k->warehousing_status_name = '';
                }

            }
        }
        return json_encode($list);
    }

    public static function count($where)
    {
        $count = Db::table('wa_purchased_vehicles') -> where($where)
                ->count();
        return $count;
    }


    /**
     * 删除
     * @param $data
     * @return int
     */
    public static function delete($condition)
    {
        return Db::table('wa_purchased_vehicles')->where($condition)->delete();
    }


    /**
     * 删除
     * @param $data
     * @return int
     */
    public static function deleteByVin($condition, $where_in)
    {
        return Db::table('wa_purchased_vehicles')->where($condition)->whereNotIn('vin', $where_in)->delete();
    }


    public static function sumsTraffic($where)
    {
        $result = Db::table('wa_purchased_vehicles')
            ->where($where)
            ->whereBetween('status', [1, 6])
//            ->where('status',1) // 已付款 1 待入库 2 已入库 3 待支付交强险 4 待上牌  5 待开二手票 6 转移待出口 7 已注销 8 入库失败
            ->sum('traffic_compulsory_insurance');
        return $result;
    }


}